﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using Newtonsoft.Json;

namespace KineticWebCrystalExport
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args = null)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                string PrintProgram, FileName, ExportPath, ExportName, RenderFormat, MethodName = "";
                string Key1, Company, ServerName, DatabaseName, UserID, Password = "";
                string POMailSubject, POMailSend, POMailReceied, POMailCC, POMailBody = "";
                bool IsDebug = false, IsRun = false, POMailIsShipMark = false, IsSendMail = false;
                string strJson = args[0];
                strJson = strJson.Replace("^^", @"""").Replace("$", " ");
                DataSet dsResult = new DataSet();
                dsResult = JsonConvert.DeserializeObject<DataSet>(strJson);
                PrintProgram = Convert.ToString(dsResult.Tables[0].Rows[0]["PrintProgram"]);
                FileName = Convert.ToString(dsResult.Tables[0].Rows[0]["FileName"]);
                ExportPath = Convert.ToString(dsResult.Tables[0].Rows[0]["ExportPath"]);
                ExportName = Convert.ToString(dsResult.Tables[0].Rows[0]["ExportName"]);
                RenderFormat = Convert.ToString(dsResult.Tables[0].Rows[0]["RenderFormat"]);
                MethodName = Convert.ToString(dsResult.Tables[0].Rows[0]["MethodName"]);
                IsDebug = Convert.ToBoolean(dsResult.Tables[0].Rows[0]["IsDebug"]);
                IsRun = Convert.ToBoolean(dsResult.Tables[0].Rows[0]["IsRun"]);

                Key1 = Convert.ToString(dsResult.Tables[0].Rows[0]["Key1"]);
                Company = Convert.ToString(dsResult.Tables[0].Rows[0]["Company"]);
                ServerName = Convert.ToString(dsResult.Tables[0].Rows[0]["ServerName"]);
                DatabaseName = Convert.ToString(dsResult.Tables[0].Rows[0]["DatabaseName"]);
                UserID = Convert.ToString(dsResult.Tables[0].Rows[0]["UserID"]);
                Password = Convert.ToString(dsResult.Tables[0].Rows[0]["Password"]);

                if (IsRun == false)
                {
                    if (string.IsNullOrWhiteSpace(Key1) == true)
                    {
                        //ExportName = Convert.ToString(dsResult.Tables[0].Rows[0]["ExportName"]) + DateTime.Now.ToString("yyyyMMddHHmmss");
                        TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", $"Start {MethodName}：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
                        if (MethodName.ToUpper() == "CrystalToPDFExportToDisk".ToUpper())
                        {
                            CrystalExportPDF.CrystalToPDFExportToDisk(PrintProgram, FileName, ExportPath, ExportName, RenderFormat, IsDebug);
                        }
                        if (MethodName.ToUpper() == "CrystalToPDFPrintToPrinter".ToUpper())
                        {
                            CrystalExportPDF.CrystalToPDFPrintToPrinter(PrintProgram, FileName, ExportPath, ExportName, RenderFormat, IsDebug);
                        }
                        if (MethodName.ToUpper() == "Preview".ToUpper())
                        {
                            CrystalDecisions.CrystalReports.Engine.ReportDocument document = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                            DataSet rptSource = new DataSet();
                            rptSource.ReadXml(FileName);
                            document.Load(PrintProgram);
                            document.SetDataSource(rptSource);
                            PubFun.ShowReport(document, "Print Preview");
                        }
                    }
                    else
                    {
                        //ExportName = Convert.ToString(dsResult.Tables[0].Rows[0]["ExportName"]);
                        POMailSubject = Convert.ToString(dsResult.Tables[0].Rows[0]["POMailSubject"]);
                        POMailSend = Convert.ToString(dsResult.Tables[0].Rows[0]["POMailSend"]);
                        POMailReceied = Convert.ToString(dsResult.Tables[0].Rows[0]["POMailReceied"]);
                        POMailCC = Convert.ToString(dsResult.Tables[0].Rows[0]["POMailCC"]);
                        POMailBody = Convert.ToString(dsResult.Tables[0].Rows[0]["POMailBody"]);
                        POMailIsShipMark = Convert.ToBoolean(dsResult.Tables[0].Rows[0]["POMailIsShipMark"]);
                        IsSendMail = Convert.ToBoolean(dsResult.Tables[0].Rows[0]["IsSendMail"]);

                        TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", $"Start {MethodName}：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
                        CrystalExportPDF.CrystalToPDFExportToDiskForKey(PrintProgram, FileName, ExportPath, ExportName, RenderFormat,Key1,Company,ServerName,DatabaseName,UserID,Password, POMailSubject,POMailSend,POMailReceied,POMailCC,POMailBody,POMailIsShipMark,IsSendMail, IsDebug);
                    }
                    TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", $"End：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
                }
                else
                {
                    Application.Run(new frmTest(dsResult));

                }
            }
            catch (Exception ex)
            {
                TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "【" + System.Reflection.MethodBase.GetCurrentMethod().Name + "】" + ex.ToString());
            }
        }
    }
}
